<template>
  <div class="wrapper">
    <el-row :gutter="40" class="panel-group">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="goPhoto">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon icon-class="album" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              相册
            </div>
            <count-to :start-val="0" :end-val="count.albumCount" :duration="500" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="goPhoto">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon icon-class="photo" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              照片
            </div>
            <count-to :start-val="0" :end-val="count.photoCount" :duration="1000" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="goFolder">
          <div class="card-panel-icon-wrapper icon-folder">
            <svg-icon icon-class="folder2" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              目录
            </div>
            <count-to :start-val="0" :end-val="count.folderCount" :duration="1500" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel" @click="goFolder">
          <div class="card-panel-icon-wrapper icon-document">
            <svg-icon icon-class="document" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              文件
            </div>
            <count-to :start-val="0" :end-val="count.documentCount" :duration="2000" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="30">
      <el-col :span="8">
        <el-card align="center">
          <p>最近上传照片</p>
          <div v-for="(item, index) in photoList" :key="item.id" style="float:left" @click="openPhoto(index)">
            <el-image :src="item.thumbnailUrl" class="image">
              <div slot="placeholder" class="image-slot">加载中<span class="dot">...</span></div>
            </el-image>
          </div>
          <div v-if="photoList.length === 0">
            <span>暂无数据</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card align="center">
          <p>最近上传文档</p>
          <el-table :data="documentList" :show-header="false" class="document_table" @row-click="openDocument">
            <el-table-column prop="name" label="文件名" class="document_name"></el-table-column>
            <el-table-column prop="createTime" align="right" label="最后修改时间" width="160"></el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card align="center">
          <p>天气 - {{ city }}</p>
          <el-row justify="space-between" type="flex">
            <el-col :span="4.2" v-for="item in weatherList" :key="item.key">
              <div class="weather_item">{{ item.week }}</div>
              <div class="weather_item"><svg-icon :icon-class="item.skycon" class="weather_icon"></svg-icon></div>
              <div class="weather_item">{{ item.skyconDesc }}</div>
              <div class="weather_item">{{ item.temperatureMax }}℃</div>
              <div class="weather_item">{{ item.temperatureMin }}℃</div>
              <div class="weather_item">{{ item.airQualityDesc }}</div>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
    <preivew-photo :photoList="photoList" ref="previewPhoto"></preivew-photo>
  </div>
</template>

<script>
import * as homeApi from '../../api/home'
import preivewPhoto from '../photo/components/preivewPhoto'
import CountTo from 'vue-count-to'
import {fmtDate2, getWeek} from '../../utils/DateUtil'
import {messageWarning} from '../../utils/message'

export default {
  name: 'Dashboard',
  components: {CountTo, preivewPhoto},
  data() {
    return {
      count: {
        albumCount: 0,
        photoCount: 0,
        folderCount: 0,
        documentCount: 0
      },
      photoList: [],
      documentList: [],
      weatherList: [],
      city: ''
    }
  },
  created() {
    this.getCount()
    this.getPhoto()
    this.getDocument()
    this.getWeather()
  },
  methods: {
    getCount() {
      homeApi.getCount().then(response => {
        this.count = response.data
      })
    },
    getPhoto() {
      homeApi.getRecentlyPhoto().then(response => {
        this.photoList = response.data
        this.photoList.forEach(item => {
          item['msrc'] = item.thumbnailUrl
          item['src'] = item.url
          item['w'] = item.width
          item['h'] = item.height
          item['alt'] = item.name
        })
      })
    },
    getDocument() {
      homeApi.getRecentlyDocument().then(response => {
        response.data.forEach(item => {
          item.createTime = fmtDate2(item.lastModifiedDate)
        })
        this.documentList = response.data
      })
    },
    getWeather() {
      homeApi.getWeather().then(response => {
        this.city = response.data.city
        response.data.weatherBeanList.forEach(item => {
          item['week'] = getWeek(item.data)
          item['temperatureMax'] = Math.round(item.temperatureMax)
          item['temperatureMin'] = Math.round(item.temperatureMin)
        })
        this.weatherList = response.data.weatherBeanList
        console.log(this.weatherList)
      })
    },
    goPhoto() {
      this.$router.push({name: 'AlbumList'})
    },
    goFolder() {
      this.$router.push({name: 'FolderList'})
    },
    openPhoto(index) {
      this.$refs.previewPhoto.openImage(index)
    },
    openDocument(row) {
      let officeUrl = 'https://view.officeapps.live.com/op/view.aspx'
      if (row.size === 0) {
        messageWarning('文件大小为0，无法打开')
        return
      }
      if (row.simpleType === 'word' || row.simpleType === 'ppt') {
        if (row.size / 1024 / 1024 > 10) {
          messageWarning('文件太大，无法在线打开')
          return
        }
        window.open(officeUrl + '?src=' + encodeURIComponent(row.url))
      } else if (row.simpleType === 'excel') {
        if (row.size / 1024 / 1024 > 5) {
          messageWarning('文件太大，无法在线打开')
        }
        window.open(officeUrl + '?src=' + encodeURIComponent(row.url))
      } else if (row.simpleType === 'pdf') {
        window.open(row.url)
      } else {
        messageWarning('不支持此格式的文件')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wrapper {
  margin: 10px;
  .allImage {
    bottom: 30px;
  }
  .image {
    cursor: pointer;
    margin: 6px;
    height: 150px;
    width: 150px;
  }
  .document_table {
    cursor: pointer;
    .document_name {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .weather_item {
    margin: 25px;
  }
  .weather_icon {
    height: 50px;
    width: 50px;
  }
}
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-folder {
        background: #f4516c;
      }

      .icon-document {
        background: #bfb955;
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-folder {
      color: #f4516c;
    }

    .icon-document {
      color: #bfb955;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width: 550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
